/*
 * Created by JFormDesigner on Thu Jun 27 10:45:20 CST 2024
 */

package scct.hanyuesheng.ui;

import scct.hanyuesheng.dao.UserDao;

import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;
import javax.swing.*;

/**
 * @author 26984
 */
public class DlgUserAdd extends JDialog {
    public DlgUserAdd(Window owner) {
        super(owner);
        initComponents();
    }

    private void btnAdd(ActionEvent e) throws SQLException {
        // TODO add your code here
//
//        获取用户输入信息
        String username=txtUser.getText();
        String pass=txtPass.getText();
        int age=Integer.parseInt(txtAge.getModel().getValue().toString());
        String gender=radMale.isSelected()?"男":"女";
        String mobile=txtMobile.getText();
        if (!username.matches("^\\w{3,8}$")){
            JOptionPane.showConfirmDialog(this,"你输入的用户名不合法，请重新输入");
            return;
        }
        if(!pass.matches("^\\w{3,8}$"))
        {
            JOptionPane.showMessageDialog(this, "请输入合法的密码");
            return;
        }
        if(age<10 || age>100)
        {
            JOptionPane.showMessageDialog(this, "请输入正确的年龄");
            return;
        }
        if(!mobile.matches("^1[3-9]\\d{9}$"))
        {
            JOptionPane.showMessageDialog(this,"请输入合法的手机号码");
            return;
        }
        UserDao userDao = new UserDao();

//      判断是否重名

        if (userDao.isDupname(username)){
            JOptionPane.showMessageDialog(this,"用户名已存在，请重新输入");
            return;
        }


        if(userDao.addUser(username,pass,gender,age,mobile)){
            JOptionPane.showMessageDialog(this,"添加成功");
        }
        this.dispose();
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - 韩月生
        tabUser = new JLabel();
        txtUser = new JTextField();
        lable2 = new JLabel();
        txtPass = new JTextField();
        label2 = new JLabel();
        txtMobile = new JTextField();
        label3 = new JLabel();
        txtAge = new JSpinner();
        label4 = new JLabel();
        radMale = new JRadioButton();
        radFemale = new JRadioButton();
        btnAdd = new JButton();
        btnCancel = new JButton();

        //======== this ========
        setTitle("\u6dfb\u52a0\u7528\u6237");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- tabUser ----
        tabUser.setText("\u7528\u6237\u540d");
        contentPane.add(tabUser);
        tabUser.setBounds(new Rectangle(new Point(25, 35), tabUser.getPreferredSize()));
        contentPane.add(txtUser);
        txtUser.setBounds(105, 28, 180, txtUser.getPreferredSize().height);

        //---- lable2 ----
        lable2.setText("\u5bc6\u7801");
        contentPane.add(lable2);
        lable2.setBounds(25, 72, lable2.getPreferredSize().width, 17);
        contentPane.add(txtPass);
        txtPass.setBounds(105, 68, 180, 30);

        //---- label2 ----
        label2.setText("\u624b\u673a\u53f7\u7801");
        contentPane.add(label2);
        label2.setBounds(25, 109, label2.getPreferredSize().width, 17);
        contentPane.add(txtMobile);
        txtMobile.setBounds(105, 103, 180, 30);

        //---- label3 ----
        label3.setText("\u5e74\u9f84");
        contentPane.add(label3);
        label3.setBounds(25, 146, label3.getPreferredSize().width, 17);
        contentPane.add(txtAge);
        txtAge.setBounds(105, 138, 180, 30);

        //---- label4 ----
        label4.setText("\u6027\u522b");
        contentPane.add(label4);
        label4.setBounds(25, 183, label4.getPreferredSize().width, 17);

        //---- radMale ----
        radMale.setText("\u7537");
        radMale.setSelected(true);
        contentPane.add(radMale);
        radMale.setBounds(new Rectangle(new Point(125, 183), radMale.getPreferredSize()));

        //---- radFemale ----
        radFemale.setText("\u5973");
        contentPane.add(radFemale);
        radFemale.setBounds(new Rectangle(new Point(220, 183), radFemale.getPreferredSize()));

        //---- btnAdd ----
        btnAdd.setText("\u6dfb\u52a0");
        btnAdd.addActionListener(e -> {try {
btnAdd(e);} catch (SQLException ex) {
    throw new RuntimeException(ex);
}});
        contentPane.add(btnAdd);
        btnAdd.setBounds(new Rectangle(new Point(60, 220), btnAdd.getPreferredSize()));

        //---- btnCancel ----
        btnCancel.setText("\u53d6\u6d88");
        contentPane.add(btnCancel);
        btnCancel.setBounds(200, 220, 78, 30);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(400, 300);
        setLocationRelativeTo(null);

        //---- buttonGroup1 ----
        var buttonGroup1 = new ButtonGroup();
        buttonGroup1.add(radMale);
        buttonGroup1.add(radFemale);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - 韩月生
    private JLabel tabUser;
    private JTextField txtUser;
    private JLabel lable2;
    private JTextField txtPass;
    private JLabel label2;
    private JTextField txtMobile;
    private JLabel label3;
    private JSpinner txtAge;
    private JLabel label4;
    private JRadioButton radMale;
    private JRadioButton radFemale;
    private JButton btnAdd;
    private JButton btnCancel;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
